/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "delay.h"
#include <stdio.h>
#include "BQ4050.h"


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

//uint8_t LA_Buffer[] = "LA";
//uint8_t LD_Buffer[] = "LD\0";
//uint8_t FA_Buffer[] = "FA\0";
//uint8_t FD_Buffer[] = "FD\0";
//uint8_t TP_Buffer[] = "TP\0";
//uint8_t SW_Buffer[] = "SWEYE\0";

uint8_t PC_Flag = 0;						//0 - �ػ� 1 - ����
uint8_t BUZZ_Flag = 0;
uint8_t LED_Flag = 0;
uint8_t PCRST_Flag = 0;
uint8_t CHG_Flag = 0;
uint8_t BTN_Flag = 0;

uint8_t Bat_Num = 50;
uint8_t Bat_Sto = 50;
uint8_t Bat_Err_Count = 0;
uint8_t Bat_Low_Flag = 0;								//1-电量低蜂鸣器响
char Bat_Send[10];
uint16_t Bat_Err_Time = 0;
char asd[15];

uint8_t PC_SHUT = 0;
uint8_t PC_Shut_Count = 0;

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
	delay_init(72);
	
//	IIC_Init();
	
	HAL_TIM_Base_Start_IT(&htim3);
//	HAL_TIM_Base_Start_IT(&htim2);
	
//	CDC_Transmit_FS("asd", 3);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	
	if(MAIN_KEY_IO_State == RESET)
	{
		HAL_Delay(80);
		if(MAIN_KEY_IO_State == RESET)
		{
			if(PC_Flag == 0)
			{
				Q4_ON;
				SYS_ON_LED_ON;
				PC_Flag = 1;
			}
//			else if(PC_DET_State != RESET && PC_Flag == 2)
//			{
//				PC_Flag = 3;
//			}
		}
	}
	
	Bat_Sto = bq40z50_read_capacity();
	if(Bat_Sto == 255)
	{
		Bat_Num = 50;
	}
	else
	{
		Bat_Num = Bat_Sto;
	}
		
  while (1)
  {
		if(DC_IN_DET_State == RESET)
		{
			BAT_STAT_LED_ON;
		}
		else
		{
			BAT_STAT_LED_OFF;
		}
		
		Bat_Sto = bq40z50_read_capacity();
		if(Bat_Sto == 255)
		{
			Bat_Err_Count ++;
			Bat_Err_Time ++;
			sprintf(asd, "ErrT:%d", Bat_Err_Time);
			CDC_Transmit_FS((uint8_t*)asd, strlen(asd));
			if(Bat_Err_Count >= 5)
			{
				Bat_Err_Count = 0;
				Bat_Num = 50;
			}
		}
		else
		{
			Bat_Num = Bat_Sto;
		}
		
		sprintf(Bat_Send, "BAT%d", Bat_Num);
		
		if(DC_IN_DET_State == RESET)
		{
			if(Bat_Num <= 2)
			{
				if(PC_DET_State != RESET)
				{
					MCU_PC_RST_SW_ON;
					PCRST_Flag = 1;
				}
				PC_Flag = 4;
			}
			else if(Bat_Num < 7)
			{
				CHG_EN1_OFF;
			}
			
			if(Bat_Num < 10)
			{
				Bat_Low_Flag = 1;
			}
		}
		
		if(Bat_Num >= 7 || DC_IN_DET_State != RESET)
		{
			CHG_EN1_ON;
		}
		
		if(Bat_Num >= 10 || DC_IN_DET_State != RESET)
		{
			Bat_Low_Flag = 0;
		}

		if(PC_DET_State == RESET)
		{
			PC_SHUT = 1;
		}
		else
		{
			PC_SHUT = 0;
			PC_Shut_Count = 0;
		}

		delay_ms(100);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	switch(GPIO_Pin)
	{
		case LA_Pin :
				HAL_Delay(80);
				if(LA_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"LA", strlen("LA"));
					TIM2 -> CNT = 0;
					BTN_Flag = 1;
					HAL_TIM_Base_Start_IT(&htim2);
				}
			break;
		case LD_Pin :
				HAL_Delay(80);
				if(LD_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"LD", strlen("LD"));
					TIM2 -> CNT = 0;
					BTN_Flag = 1;
					HAL_TIM_Base_Start_IT(&htim2);
				}
				break;
		case TP_Pin :
				HAL_Delay(80);
				if(TP_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"TP", strlen("TP"));
				}
			break;
		case FA_Pin :
				HAL_Delay(80);
				if(FA_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"FA", strlen("FA"));
					TIM2 -> CNT = 0;
					BTN_Flag = 1;
					HAL_TIM_Base_Start_IT(&htim2);
				}
			break;
		case FD_Pin :
				HAL_Delay(80);
				if(FD_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"FD", strlen("FD"));
					TIM2 -> CNT = 0;
					BTN_Flag = 1;
					HAL_TIM_Base_Start_IT(&htim2);
				}
			break;
		case SW_EYES_Pin :
				HAL_Delay(80);
				if(SW_State == GPIO_PIN_RESET)
				{
					CDC_Transmit_FS((uint8_t*)"SWEYE", strlen("SWEYE"));
				}
			break;
		case MAIN_KEY_IO_Pin :
				HAL_Delay(80);
				if(MAIN_KEY_IO_State == GPIO_PIN_RESET)
				{
					TIM3 -> CNT = 0;
					if(PC_Flag == 0)
					{
						Q4_ON;
						SYS_ON_LED_ON;
						PC_Flag = 1;
					}
					else if(PC_Flag == 2)
					{
						PC_Flag = 3;
					}
				}
			break;
		case Magnetic_MCU_Pin :
				HAL_Delay(80);
				if(Magnetic_MCU_State != GPIO_PIN_RESET)
				{
					Q4_ON;
					if((DC_IN_DET_State != RESET) || (Bat_Num >= 7))
					{
//						CHG_EN1_ON;
					}
					else
					{
						CHG_EN1_OFF;
						TIM3 -> CNT = 0;
						BUZZ_OFF;
						CHG_Flag = 1;
					}
				}
			break;
	}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	static uint8_t PC_Count 			= 0;
	static uint8_t BUZZ_Count 		= 0;
	static uint8_t PCRST_Count 		= 0;
	static uint8_t BUZZ_SOS_Flag 	= 0;
	static uint8_t BUZZ_SOS_Count = 0;
	static uint8_t CHG_SOS_Count = 0;
	static uint8_t LED_Count = 0;
	static uint16_t PC_DET_Count 	= 0;
	static uint8_t Bat_Count = 0;
	static uint8_t BUZZ_ERR_Flag = 0;
	static uint8_t BUZZ_ERR_Count = 0;
	
	if(htim == (&htim2))
	{
		switch(BTN_Flag)
		{
			case 1 :
					BTN_Flag = 2;
				break;
			case 2 :
					BTN_Flag = 3;
				break;
			case 3 :
					if(LA_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"LA", strlen("LA"));
					}
					else if(LD_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"LD", strlen("LD"));
					}
					else if(FA_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"FA", strlen("FA"));
					}
					else if(FD_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"FD", strlen("FD"));
					}
					else
					{
						HAL_TIM_Base_Stop_IT(&htim2);
						BTN_Flag = 0;
					}
					BTN_Flag = 4;
				break;
			case 4 :
					if(LA_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"LA", strlen("LA"));
					}
					else if(LD_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"LD", strlen("LD"));
					}
					else if(FA_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"FA", strlen("FA"));
					}
					else if(FD_State == RESET)
					{
						CDC_Transmit_FS((uint8_t*)"FD", strlen("FD"));
					}
					else
					{
						HAL_TIM_Base_Stop_IT(&htim2);
						BTN_Flag = 0;
					}
				break;
		}
	}
	
	if(htim == (&htim3))
	{
		switch(PC_Flag)
		{
			case 1 :
					if(MAIN_KEY_IO_State == RESET)
					{
						PC_Count ++;
						if(PC_Count >= 20)
						{
							PC_Count = 0;
							if((DC_IN_DET_State != RESET) || (Bat_Num > 3))
							{
								BUZZ_ON;
								BUZZ_Flag = 1;
								SYS_ON_LED_ON;
								PC_Flag = 2;
								if(PC_DET_State == RESET)
								{
									MCU_PC_RST_SW_ON;
									PCRST_Flag = 1;
								}
							}
							else
							{
								BUZZ_SOS_Flag = 1;
								BUZZ_ON;
								PC_Flag = 10;
							}
						}
					}
					else
					{
						PC_Count = 0;
						SYS_ON_LED_OFF;
						Q4_OFF;
					}
				break;
			case 3 :
					if(MAIN_KEY_IO_State == RESET)
					{
						PC_Count ++;
						if(PC_Count >= 20)
						{
							SYS_ON_LED_ON;
							LED_Flag = 1;
							PC_Count = 0;
							BUZZ_ON;
							BUZZ_Flag = 1;
							if(PC_DET_State != RESET)
							{
								MCU_PC_RST_SW_ON;
								PCRST_Flag = 1;
							}
							PC_Flag = 4;
						}
					}
				break;
			case 4 :
					PC_DET_Count ++;
					if(PC_DET_State == RESET)
					{
						Q4_OFF;
						PC_Flag = 0;
					}
					if(PC_DET_Count > 300)
					{
						PC_DET_Count = 0;
						Q4_OFF;
						PC_Flag = 0;
					}
				break;
		}
		
		if(BUZZ_Flag == 1)
		{
			BUZZ_Count ++;
			if(BUZZ_Count > 7)
			{
				BUZZ_OFF;
				BUZZ_Flag = 0;
				BUZZ_Count = 0;
			}
		}
		
		if(PCRST_Flag == 1)
		{
			PCRST_Count ++;
			if(PCRST_Count > 6)
			{
				MCU_PC_RST_SW_OFF;
				PCRST_Flag = 2;
				PCRST_Count = 0;
			}
		}
		
		if(BUZZ_SOS_Flag == 1)
		{
			GPIOB -> ODR ^= GPIO_PIN_0;
			BUZZ_SOS_Count ++;
			if(BUZZ_SOS_Count >= 6)
			{
				BUZZ_SOS_Count = 0;
				BUZZ_SOS_Flag = 0;
				Q4_OFF;
			}
		}
		
		if(CHG_Flag == 1)
		{
			GPIOB -> ODR ^= GPIO_PIN_0;
			CHG_SOS_Count ++;
			if(CHG_SOS_Count >= 10)
			{
				CHG_SOS_Count = 0;
				CHG_Flag = 0;
			}
		}
		
		if(LED_Flag == 1)
		{
			GPIOC -> ODR ^= GPIO_PIN_13;
			LED_Count ++;
			if(LED_Count >= 10)
			{
				LED_Count = 0;
				LED_Flag = 0;
			}
		}
		
		Bat_Count ++;
		if(Bat_Count > 30)
		{
			Bat_Count = 0;
			CDC_Transmit_FS((uint8_t*)Bat_Send, strlen(Bat_Send));
		}
		
		if(Bat_Low_Flag == 1)
		{
			BUZZ_ERR_Count ++;
			switch(BUZZ_ERR_Flag)
			{
				case 0 :
						if(BUZZ_ERR_Count >= 2)
						{
							BUZZ_OFF;
							BUZZ_ERR_Count = 0;
							BUZZ_ERR_Flag = 1;
						}
					break;
				case 1 :
						if(BUZZ_ERR_Count >= 18)
						{
							BUZZ_ON;
							BUZZ_ERR_Count = 0;
							BUZZ_ERR_Flag = 0;
						}
					break;
			}
		}
		
		if(PC_SHUT == 1)
		{
			PC_Shut_Count ++;
			if(PC_Shut_Count >= 50)
			{
				if(PC_DET_State == RESET)
				{
					Q4_OFF;
				}
				else
				{
					PC_Shut_Count = 0;
					PC_SHUT = 0;
				}
			}
		}
		
	}
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
